<?php

class UserOrderController extends Zend_Controller_Action
{
    /**
     * @return void
     */
    public function preDispatch()
    {
        $user = JFactory::getUser();
        $action = $this->_request->getActionName();
        switch ($action) {
            case 'index':
                if ($user->IsGuest) {
                    $this->redirect('/index/index');
                }
                break;
        }
    }

    /**
     * @return void
     */
    public function init()
    {
    }

    /**
     * @return void
     */
    public function indexAction()
    {
        // BEGIN: Tạo đối tượng Paginator
        $user = JFactory::getUser();
        $userID = $user->ID;

        $dbSelect = JDbAdapter::factory()
            ->select()
            ->from('ORDERS')
            ->where('UserID = ?', $userID);

        $paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbSelect($dbSelect)
        );

        $paginator->setItemCountPerPage(5);
        // END: Tạo đối tượng Paginator

        // Get and set current page
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        $orders = $paginator->getItemsByPage($page);

        // Asign data for view
        $this->view->orders = $orders;
        $this->view->paginator = $paginator;

        $numberOfProcessedOrders = $user->getNumberOfProcessedOrders();
        $this->view->numberOfProcessedOrders = $numberOfProcessedOrders;
    }
}
